package com.apache.client;

import com.apache.tools.DateUtils;
import com.apache.tools.LogUtil;
import com.apache.tools.StrUtil;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Date;

/**
 * AES加解密算法
 */
public class AesClientUtil {

	/**
	 * AES加解密
	 */
	private static final String ALGORITHM = "AES";
	/**
	 * 默认加密的KEY
	 */
	private static final String KEY_DEFAULT = "V4Ius.Soft08GUA9";//8G5M4Ff9hel8fUA9

	/**
	 * 工作模式：ECB
	 */
	private static final String TRANSFORM_ECB_PKCS5 = "AES/ECB/PKCS5Padding";


	private static AesClientUtil instance = new AesClientUtil();

	private AesClientUtil(){}

	public static AesClientUtil getInstance(){
		return instance;
	}

	/**
	 * 加密
	 * @param  src 加密字符串
	 * @return 加密后的字符串
	 */
	public String encryptEcbMode(String src){
		try{
			return encryptEcbMode(src,KEY_DEFAULT);
		}catch (Exception e){
			return "";
		}
	}
	/**
	 * 加密
	 * @param  src 加密字符串
	 * @param  key 密钥
	 * @return 加密后的字符串
	 */
	public String encryptEcbMode(String src, String key) {
		// 判断密钥是否为空
		if (StrUtil.isNull(key)) {
			key = KEY_DEFAULT;
		}
		try {
			// 密钥补位
			int plus= 16-key.length();
			byte[] data = key.getBytes("utf-8");
			byte[] raw = new byte[16];
			byte[] plusbyte={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
			for(int i=0;i<16;i++) {
				if (data.length > i)
					raw[i] = data[i];
				else
					raw[i] = plusbyte[plus];
			}
			SecretKeySpec skeySpec = new SecretKeySpec(raw, ALGORITHM);
			Cipher cipher = Cipher.getInstance(TRANSFORM_ECB_PKCS5);	// 算法/模式/补码方式
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
			byte[] encrypted = cipher.doFinal(src.getBytes("utf-8"));
			return parseByte2HexStr(encrypted);
		} catch (Exception ex) {
			return src;
		}
	}

	/**
	 * 解密
	 * @param  src 解密字符串
	 * @return 解密后的字符串
	 */
	public String decryptEcbMode(String src) {
		return decryptEcbMode(src,KEY_DEFAULT);
	}

	/**
	 * 解密
	 * @param  src 解密字符串
	 * @param  key 密钥
	 * @return 解密后的字符串
	 */
	public String decryptEcbMode(String src, String key) {
		try {
			// 判断Key是否正确
			if (StrUtil.isNull(key)) {
				key = KEY_DEFAULT;
			}
			// 密钥补位
			int plus= 16-key.length();
			byte[] data = key.getBytes("utf-8");
			byte[] raw = new byte[16];
			byte[] plusbyte={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
			for(int i=0;i<16;i++) {
				if (data.length > i)
					raw[i] = data[i];
				else
					raw[i] = plusbyte[plus];
			}
			SecretKeySpec skeySpec = new SecretKeySpec(raw, ALGORITHM);
			Cipher cipher = Cipher.getInstance(TRANSFORM_ECB_PKCS5);
			cipher.init(Cipher.DECRYPT_MODE, skeySpec);
			byte[] encrypted1 = parseHexStr2Byte(src);
			try {
				byte[] original = cipher.doFinal(encrypted1);
				String originalString = new String(original,"utf-8");
				return originalString;
			} catch (Exception e) {
				return src;
			}
		} catch (Exception ex) {
			return src;
		}
	}


	/**
	 * 将二进制转换成16进制
	 * @method parseByte2HexStr
	 * @param buf
	 */
	private String parseByte2HexStr(byte buf[]){
		StringBuffer sb = new StringBuffer();
		for(int i = 0; i < buf.length; i++){
			String hex = Integer.toHexString(buf[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			sb.append(hex.toUpperCase());
		}
		return sb.toString();
	}

	/**
	 * 将16进制转换为二进制
	 * @method parseHexStr2Byte
	 * @param hexStr
	 */
	private byte[] parseHexStr2Byte(String hexStr){
		if(hexStr.length() < 1)
			return null;
		byte[] result = new byte[hexStr.length()/2];
		for (int i = 0;i< hexStr.length()/2; i++) {
			int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
			int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
			result[i] = (byte) (high * 16 + low);
		}
		return result;
	}

}
